package com.bsi.app.widget;

import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SmsManager;

public class SMSUtil {
	public static void toSendSMSActivity(Activity activiy, String content) {
		Uri uri = Uri.parse("smsto:");
		Intent it = new Intent(Intent.ACTION_SENDTO, uri);
		it.putExtra("sms_body", content);
		activiy.startActivity(it);
	}

	public static void toSendSMSActivity(Activity activiy, String content,
			String phoneNo) {
		Uri uri = Uri.parse("smsto:" + phoneNo);
		Intent it = new Intent(Intent.ACTION_SENDTO, uri);
		it.putExtra("sms_body", content);
		activiy.startActivity(it);
	}

	public static void sendMsgInBackground(Context context, String phoneNumber,
			String content) {
		SmsManager sms = SmsManager.getDefault();
		phoneNumber="+86"+phoneNumber;
		Intent sendIntent = new Intent("SEND_SMS_ACTION");
		PendingIntent sendPI = PendingIntent.getBroadcast(context, 0,
				sendIntent, 0);
		Intent deliverIntent = new Intent("DELIVERED_SMS_ACTION");
		PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,
				deliverIntent, 0);
		if (content.getBytes().length > 140) {
			ArrayList<String> msgList = sms.divideMessage(content);
			for (String msg : msgList) {
				sms.sendTextMessage(phoneNumber, null, msg, sendPI, deliverPI);
			}
		} else {
			sms.sendTextMessage(phoneNumber, null, content,sendPI, deliverPI);
		}
	}
}
